<?php
    session_start();

    include('include/functions.inc');

    if (isset($_POST["login"]) && isset($_POST["password"])){
        $nombre = $_POST["login"];
	    $pass = $_POST["password"];
        if (loginUser($nombre,$pass)){
            $_SESSION['usuario'] = $nombre;
            $_SESSION['password'] = $pass;
            if (isset($_COOKIE['lastLogin'])){
                $_SESSION['lastLogin'] = $_COOKIE['lastLogin'];
                setcookie('lastLogin',time(),time() + 365 * 24 * 60 * 60);
            }else{
                setcookie('lastLogin',time(),time() + 365 * 24 * 60 * 60);
                $_SESSION['lastLogin'] = $_COOKIE['lastLogin'];
            }

            if ($_POST["rememberMe"]){
                setcookie('usuario',$nombre,time() + 365 * 24 * 60 * 60);
                setcookie('password',$pass,time() + 365 * 24 * 60 * 60);
                header("Location: usuarioRegistrado.php");
            }else{
                header("Location: usuarioRegistrado.php");
            }
        }else{
            header("Location: index.php");
        }
    }else{
        header("Location: index.php");
    }

?>